package handlers

import (
	"ai/internal/db/models"
	"ai/internal/response"
	"ai/internal/service"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

var activityService *service.ActivityService

func SetActivityService(svc *service.ActivityService) {
	activityService = svc
}

// StartPracticeHandler godoc
// @Summary Start a practice session
// @Description Start a practice session for a user with a specific question type
// @Tags activity
// @Accept json
// @Produce json
// @Param user_id query int true "User ID"
// @Param question_type query string true "Question Type"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]interface{}
// @Router /activity/practice [post]
func StartPracticeHandler(c *gin.Context) {
	userID, err := strconv.ParseUint(c.Query("user_id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "Invalid user ID")
		return
	}

	questionType := c.Query("question_type")
	if questionType == "" {
		response.Error(c, http.StatusBadRequest, "Question type is required")
		return
	}

	err = activityService.StartPractice(uint(userID), questionType)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, gin.H{"message": "Practice session started"})
}

// StartExamHandler godoc
// @Summary Start an exam session
// @Description Start an exam session for a user
// @Tags activity
// @Accept json
// @Produce json
// @Param user_id query int true "User ID"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]interface{}
// @Router /activity/exam [post]
func StartExamHandler(c *gin.Context) {
	userID, err := strconv.ParseUint(c.Query("user_id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "Invalid user ID")
		return
	}

	err = activityService.StartExam(uint(userID))
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, gin.H{"message": "Exam session started"})
}

// GetWrongSetHandler godoc
// @Summary Get user's wrong set
// @Description Retrieve the wrong set for a user
// @Tags activity
// @Produce json
// @Param user_id query int true "User ID"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]interface{}
// @Router /activity/wrongset [get]
func GetWrongSetHandler(c *gin.Context) {
	userID, err := strconv.ParseUint(c.Query("user_id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "Invalid user ID")
		return
	}

	wrongSet, err := activityService.GetWrongSet(uint(userID))
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, gin.H{"wrong_set": wrongSet})
}

// ResolveWrongQuestionHandler godoc
// @Summary Resolve a wrong question
// @Description Mark a wrong question as resolved for a user
// @Tags activity
// @Accept json
// @Produce json
// @Param user_id query int true "User ID"
// @Param question_id query int true "Question ID"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]interface{}
// @Router /activity/wrongset/resolve [post]
func ResolveWrongQuestionHandler(c *gin.Context) {
	userID, err := strconv.ParseUint(c.Query("user_id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "Invalid user ID")
		return
	}

	questionID, err := strconv.ParseUint(c.Query("question_id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "Invalid question ID")
		return
	}

	err = activityService.ResolveWrongQuestion(uint(userID), uint(questionID))
	if err != nil {
		response.ResponseWithCode(c, "error", err.Error(), nil)
		return
	}

	response.ResponseWithCode(c, "success", "Wrong question resolved", nil)
}

// GetQuestionsByStatusHandler godoc
// @Summary Get questions by status
// @Description Retrieve questions for a user by their status
// @Tags activity
// @Produce json
// @Param user_id query int true "User ID"
// @Param status query string true "Question Status"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]interface{}
// @Router /activity/questions/status [get]
func GetQuestionsByStatusHandler(c *gin.Context) {
	userID, err := strconv.ParseUint(c.Query("user_id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "Invalid user ID")
		return
	}

	status := models.QuestionStatus(c.Query("status"))
	if status == "" {
		response.Error(c, http.StatusBadRequest, "Status is required")
		return
	}

	questions, err := activityService.GetQuestionsByStatus(uint(userID), status)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, gin.H{"questions": questions})
}

// UpdateQuestionStatusHandler godoc
// @Summary Update question status
// @Description Update the status of a question for a user
// @Tags activity
// @Accept json
// @Produce json
// @Param user_id query int true "User ID"
// @Param question_id query int true "Question ID"
// @Param status query string true "Question Status"
// @Success 200 {object} map[string]interface{}
// @Failure 400 {object} map[string]interface{}
// @Router /activity/questions/status/update [post]
func UpdateQuestionStatusHandler(c *gin.Context, services *service.ServiceManager) {
	userID, err := strconv.ParseUint(c.Query("user_id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "Invalid user ID")
		return
	}

	questionID, err := strconv.ParseUint(c.Query("question_id"), 10, 32)
	if err != nil {
		response.Error(c, http.StatusBadRequest, "Invalid question ID")
		return
	}

	status := models.QuestionStatus(c.Query("status"))
	if status == "" {
		response.Error(c, http.StatusBadRequest, "Status is required")
		return
	}

	err = activityService.UpdateQuestionStatus(uint(userID), uint(questionID), status)
	if err != nil {
		response.Error(c, http.StatusInternalServerError, err.Error())
		return
	}

	response.Success(c, gin.H{"message": "Question status updated"})
}
